﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Helpers;
using System.Web.Mvc;
using LSEntity;
using LSUtility;

namespace LSBLL
{
    public static class MyHtmlHelper
    {
        #region ListFormatTable

        public static MvcHtmlString ListFormatTable<T>(this List<T> sources, string tempXNPath, string timeFormat = "yyyy-MM-dd HH:mm") where T : class
        {
            if (sources == null) return new MvcHtmlString(String.Empty);
            var result = String.Empty;
            var arr = BaseTool.GetLoopArray(BaseTool.GetStringFromTxt(tempXNPath));
            var temp = arr[0];
            var head = String.Empty;
            var feet = String.Empty;
            if (arr.Length == 3)
            {
                head = arr[0];
                temp = arr[1];
                feet = arr[2];
            }
            result += head;
            foreach (var model in sources)
            {
                var str = temp;
                model.ObjGetProperty().ForEach(m =>
                                                   {
                                                       str = str.Replace(String.Format("{{{0}}}", m),
                                                                         model.ObjGetValue(m)
                                                                              .Obj2Str(timeFormat));
                                                   });
                result += str;
            }
            result += feet;
            return new MvcHtmlString(result);
        }

        #endregion

        #region MySelect

        public static MvcHtmlString MySelect<T, T1, T2>(this IEnumerable<T> sources,
                                                        Expression<Func<T, T1>> value,
                                                        Expression<Func<T, T2>> text,
                                                        string name = null,
                                                        object htmlAttributes = null,
                                                        CommonList<T1, T2> defaultSelect = null,
                                                        string selectValue = null
            ) where T : class
        {
            if (sources == null) return new MvcHtmlString(String.Empty);
            if (String.IsNullOrEmpty(name)) name = value.LamdaGetName();
            var str = "<select id=\"" + name + "\" name=\"" + name + "\" {0}>";
            if (htmlAttributes != null)
            {
                var nameList = htmlAttributes.ObjGetProperty();
                var attr = String.Empty;
                nameList.ForEach(m =>
                                     {
                                         attr += String.Format(" {0}=\"{1}\"", m, htmlAttributes.ObjGetValue(m));
                                     });
                str = String.Format(str, attr);
            }
            else
            {
                str = String.Format(str, String.Empty);
            }
            str += sources.ListToOption(value, text, defaultSelect, selectValue);
            str += "</select>";
            return new MvcHtmlString(str);
        }

        #endregion

        public static MvcHtmlString HtmlTemp<T>(this T t, string xnPathFile)
        {
            var temp = BaseTool.GetStringFromTxt(xnPathFile);
            return new MvcHtmlString(t.TempFormat(temp));
        }

        #region QuestionOTPreviewHtml

        public static MvcHtmlString QuestionOTPreviewHtml(this QuestionOT sources, string xnPath)
        {
            if (sources == null) return new MvcHtmlString(String.Empty);
            var resultStr = String.Empty;
            int i = 1, j = 1;
            var questionTypeTemp = BaseTool.GetStringFromTxt(xnPath + "QuestionType.cshtml");
            foreach (var qotList in sources.QuestionForOTList)
            {
                var quesType = String.Empty;
                var questionTemp = String.Empty;
                foreach (var ot in qotList)
                {
                    if (String.IsNullOrEmpty(quesType))
                    {
                        questionTemp = BaseTool.GetStringFromTxt(xnPath +
                                                                 String.Format("Question{0}.cshtml",
                                                                               ot.Question.QuestionTypeId));
                        quesType = QuestionBLL.QuestionTypeGetDescription(ot.Question.QuestionTypeId,
                                                                          (int)ot.Question.CurriculumId);
                        var arr = quesType.Split('|');
                        resultStr += new QuestionGenerateHtml.TempQuestionType
                                         {
                                             Num = ((Number)i).ToString(),
                                             TypeId = ot.Question.QuestionTypeId,
                                             TypeTitle = arr[0],
                                             Mp3 = arr.Length == 2 ? Mp3Play(arr[1]) : String.Empty
                                         }.TempFormat(questionTypeTemp);
                        i++;
                    }
                    if (String.IsNullOrEmpty(quesType) || String.IsNullOrEmpty(questionTemp)) continue;
                    resultStr += new QuestionGenerateHtml.TempQuestion
                                     {
                                         QuestionId = ot.Question.QuestionId,
                                         QuestionTitle = ot.Question.QuestionTitle,
                                         QuestionTypeId = ot.Question.QuestionTypeId,
                                         Mp3 = Mp3Play(ot.Question.Sound)
                                     }.TempFormat(questionTemp);
                    j++;
                }
            }
            return new MvcHtmlString(resultStr);
        }

        #endregion

        #region Mp3Play

        public static string Mp3Play(string soundName, string filename = "playsound_all.swf",
                                     string path = "/Content/soundswf/", int width = 16, int height = 15,
                                     string mp3Path = "soundpath=/sound/EFLQuest/")
        {
            var sound = new StringBuilder("");
            sound.Append("<object id=\"FlashID\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"" +
                         width + "\" height=\"" + height + "\">");
            sound.Append("  <param name=\"movie\" value=\"" + path + filename + "?" + soundName + "\" />");
            sound.Append("  <param name=\"quality\" value=\"high\" />");
            sound.Append("  <param name=\"wmode\" value=\"opaque\" />");
            sound.Append("  <param name=\"swfversion\" value=\"9.0.45.0\" />");
            sound.Append("  <param name=\"menu\" value=\"false\">");

            if (!string.IsNullOrEmpty(mp3Path)) sound.Append("  <param name=\"FlashVars\" value=\"" + mp3Path + "\">");
            sound.Append("  <!--[if !IE]>-->");
            sound.Append("  <object type=\"application/x-shockwave-flash\" data=\"" + path + filename + "?" + soundName +
                         "\" width=\"" + width + "\" height=\"" + height + "\">");
            sound.Append("    <!--<![endif]-->");
            sound.Append("    <param name=\"quality\" value=\"high\" />");
            sound.Append("    <param name=\"wmode\" value=\"opaque\" />");
            sound.Append("    <param name=\"swfversion\" value=\"9.0.45.0\" />");

            if (!string.IsNullOrEmpty(mp3Path)) sound.Append("  <param name=\"FlashVars\" value=\"" + mp3Path + "\">");
            sound.Append("    <!--[if !IE]>-->");
            sound.Append("  </object>");
            sound.Append("  <!--<![endif]-->");
            sound.Append("</object>");
            return sound.ToString();
        }

        #endregion
    }
}
